// Copyright (c) eBPF for Windows contributors
// SPDX-License-Identifier: MIT

#pragma once

#include "ebpf_result.h"

typedef _Return_type_success_(return >= 0) LONG NTSTATUS;

// If status values are already defined, do not include ntstatus.h.
// This avoids macro redefinitions errors when ebpf_error.h is included in other files that include these status codes.
#ifndef STATUS_SUCCESS
#include <ntstatus.h>
#endif

#if !defined(_AMD64_)
#define _AMD64_
#endif


// Mapping from each eBPF result to NTSTATUS
// (and also show the Win32 error code that the NTSTATUS maps to).
// This should be consistent with win32_error_code_to_ebpf_result()
// in include\ebpf_utilities.h.
static const NTSTATUS _ebpf_result_mapping[] = {
    /* EBPF_SUCCESS */ (NTSTATUS)STATUS_SUCCESS /* ERROR_SUCCESS */,
    /* EBPF_VERIFICATION_FAILED */ (NTSTATUS)STATUS_VERIFIER_STOP /* ERROR_VERIFIER_STOP */,
    /* EBPF_JIT_COMPILATION_FAILED */ (NTSTATUS)STATUS_NONE_MAPPED /* ERROR_NONE_MAPPED */,
    /* EBPF_PROGRAM_LOAD_FAILED */ (NTSTATUS)STATUS_GENERIC_COMMAND_FAILED /* ERROR_GENERIC_COMMAND_FAILED */,
    /* EBPF_INVALID_FD */ (NTSTATUS)STATUS_INVALID_HANDLE /* ERROR_INVALID_HANDLE */,
    /* EBPF_INVALID_OBJECT */ (NTSTATUS)STATUS_INVALID_DEVICE_REQUEST /* ERROR_INVALID_FUNCTION */,
    /* EBPF_INVALID_ARGUMENT */ (NTSTATUS)STATUS_INVALID_PARAMETER /* ERROR_INVALID_PARAMETER */,
    /* EBPF_OBJECT_NOT_FOUND */ (NTSTATUS)STATUS_OBJECT_PATH_NOT_FOUND /* ERROR_PATH_NOT_FOUND */,
    /* EBPF_OBJECT_ALREADY_EXISTS */ (NTSTATUS)STATUS_OBJECTID_EXISTS /* ERROR_OBJECT_ALREADY_EXISTS */,
    /* EBPF_FILE_NOT_FOUND */ (NTSTATUS)STATUS_NO_SUCH_FILE /* ERROR_FILE_NOT_FOUND */,
    /* EBPF_ALREADY_PINNED */ (NTSTATUS)STATUS_OBJECT_NAME_EXISTS /* ERROR_OBJECT_NAME_EXISTS */,
    /* EBPF_NOT_PINNED */ (NTSTATUS)STATUS_RESOURCE_NOT_OWNED /* ERROR_NOT_OWNER */,
    /* EBPF_NO_MEMORY */ (NTSTATUS)STATUS_NO_MEMORY /* ERROR_NOT_ENOUGH_MEMORY */,
    /* EBPF_PROGRAM_TOO_LARGE */ (NTSTATUS)STATUS_TOO_MANY_COMMANDS /* ERROR_TOO_MANY_CMDS */,
    /* EBPF_RPC_EXCEPTION */ (NTSTATUS)RPC_NT_CALL_FAILED /* RPC_S_CALL_FAILED */,
    /* EBPF_ALREADY_INITIALIZED */ (NTSTATUS)STATUS_ALREADY_INITIALIZED /* ERROR_ALREADY_INITIALIZED */,
    /* EBPF_ELF_PARSING_FAILED */ (NTSTATUS)STATUS_INVALID_IMAGE_FORMAT /* ERROR_BAD_EXE_FORMAT */,
    /* EBPF_FAILED */ (NTSTATUS)STATUS_UNSUCCESSFUL /* ERROR_GEN_FAILURE */,
    /* EBPF_OPERATION_NOT_SUPPORTED */ (NTSTATUS)STATUS_NOT_SUPPORTED /* ERROR_NOT_SUPPORTED */,
    /* EBPF_KEY_NOT_FOUND */ (NTSTATUS)STATUS_NOT_FOUND /* ERROR_NOT_FOUND */,
    /* EBPF_ACCESS_DENIED */ (NTSTATUS)STATUS_ACCESS_DENIED /* ERROR_ACCESS_DENIED */,
    /* EBPF_BLOCKED_BY_POLICY */ (NTSTATUS)STATUS_CONTENT_BLOCKED /* ERROR_CONTENT_BLOCKED */,
    /* EBPF_ARITHMETIC_OVERFLOW */ (NTSTATUS)STATUS_INTEGER_OVERFLOW /* ERROR_ARITHMETIC_OVERFLOW */,
    /* EBPF_EXTENSION_FAILED_TO_LOAD */ (NTSTATUS)STATUS_DRIVER_UNABLE_TO_LOAD /* ERROR_BAD_DRIVER */,
    /* EBPF_INSUFFICIENT_BUFFER */ (NTSTATUS)STATUS_BUFFER_OVERFLOW /* ERROR_MORE_DATA */,
    /* EBPF_NO_MORE_KEYS */ (NTSTATUS)STATUS_NO_MORE_MATCHES /* ERROR_NO_MORE_MATCHES */,
    /* EBPF_KEY_ALREADY_EXISTS */ (NTSTATUS)STATUS_ALREADY_REGISTERED /* ERROR_INTERNAL_ERROR, but ought to be ERROR_ALREADY_REGISTERED */,
    /* EBPF_NO_MORE_TAIL_CALLS */ (NTSTATUS)STATUS_TOO_MANY_NODES /* ERROR_TOO_MANY_NAMES */,
    /* EBPF_PENDING */ (NTSTATUS)STATUS_PENDING /* ERROR_IO_PENDING */,
    /* EBPF_OUT_OF_SPACE */ (NTSTATUS)STATUS_INSUFFICIENT_RESOURCES /* ERROR_NO_SYSTEM_RESOURCES */,
    /* EBPF_CANCELED */ (NTSTATUS)STATUS_CANCELLED /* ERROR_OPERATION_ABORTED */,
    /* EBPF_INVALID_POINTER */ (NTSTATUS)STATUS_ACCESS_VIOLATION /* ERROR_NOACCESS */,
    /* EBPF_TIMEOUT */ (NTSTATUS)STATUS_TIMEOUT /* ERROR_TIMEOUT */,
    /* EBPF_STALE_ID */ (NTSTATUS)STATUS_INVALID_DEVICE_STATE /* ERROR_BAD_COMMAND */,
    /* EBPF_INVALID_STATE */ (NTSTATUS)STATUS_INVALID_STATE_TRANSITION /* ERROR_INVALID_STATE */,
};

/**
 * @brief Map an ebpf_result_t to a generic NTSTATUS code.
 *
 * @param[in] result ebpf_result_t to map.
 * @return The generic NTSTATUS code.
 */
static inline NTSTATUS
ebpf_result_to_ntstatus(ebpf_result_t result)
{

    if (result < 0) {
        return (NTSTATUS)STATUS_UNSUCCESSFUL;
    }
    if (result > ARRAYSIZE(_ebpf_result_mapping)) {
        return (NTSTATUS)STATUS_UNSUCCESSFUL;
    }
    return _ebpf_result_mapping[result];
}